package com.fitness.front.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.front.convertor.MemberConvertor;
import com.fitness.front.convertor.MessageConverter;
import com.fitness.front.mapper.MemberMapper;
import com.fitness.front.mapper.MessageMapper;
import com.fitness.front.modal.member.entity.Member;
import com.fitness.front.modal.member.vo.MemberVO;
import com.fitness.front.modal.message.entity.Message;
import com.fitness.front.modal.message.vo.MessageVO;
import com.fitness.front.service.IMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MessageImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    private final MessageConverter messageConverter;
    private final MemberMapper memberMapper;
    private final MemberConvertor memberConvertor;

    @Override
    public List<MessageVO> messageList(Long from, Long to) {
        return this.baseMapper.messageList(from,to);
    }

    @Override
    public Integer noReadMessageCount(Long memberId) {
        //先查端对端的未读消息数
        Integer notReadCount = this.baseMapper.e2eMessageNotReadCount(memberId);
        //再查群发的未读消息数
        //a.先查群发消息数
        Integer groupCount = this.baseMapper.groupMessageCount();
        //b.再查已读的消息数
        Integer groupReadCount = 0;
        Integer groupNotReadCount = 0;
        if(groupCount > 0){
            groupReadCount = this.baseMapper.groupReadMessageCount(memberId);
            groupNotReadCount = groupCount - groupReadCount;
        }
        return notReadCount + groupNotReadCount;
    }

    @Override
    public List<MemberVO> messageMembers(Long memberId) {
        List<Member> members = this.baseMapper.messageMembers(memberId);
        return memberConvertor.entities2Vos(members);
    }
}
